package ru.ssau.customerManagement.model.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.*;

/**
 * Author: illabefat
 * Time: 02.11.2014
 */
@XmlRootElement(name="customerLocation")
@XmlAccessorType(XmlAccessType.FIELD)

@Entity
@Table(name = "CUSTOMER_LOCATIONS")
@NamedQueries(
{
    @NamedQuery(name = "CustomerLocation.allLocations", query = "SELECT l FROM CustomerLocation l"),
    @NamedQuery(name = "CustomerLocation.byAddress", query = "SELECT l FROM CustomerLocation l WHERE l.address like :address"),
    @NamedQuery(name = "CustomerLocation.byCustomer", query = "SELECT l FROM CustomerLocation l WHERE l.customerId = :customer_id")
})
public class CustomerLocation implements Serializable{
    private static final long serialVersionUID = -7244701227796930714L;

    @XmlAttribute
    @Id
    @NotNull
    @Column(name = "ID")
    private Long id;
    
    @XmlAttribute
    @NotNull
    @Column(name = "CUSTOMER_ID")
    private Long customerId;
    
    @XmlAttribute
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "ADDRESS")
    private String address;

    public CustomerLocation(){}
    public CustomerLocation(Long id, Long customerId) {
        this(id, customerId, "");
    }

    public CustomerLocation(Long id, Long customerId, String address) {
        this.id = id;
        this.customerId = customerId;
        this.address = address;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {this.id=id; }

    public Long getCustomerId() {
        return customerId;
    }
    public void setCustomerId(Long customerId) {this.customerId=customerId; }

    
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CustomerLocation)) return false;

        CustomerLocation that = (CustomerLocation) o;

        if (!id.equals(that.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
    
    @XmlRootElement(name="customerLocations")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class CustomerLocationsList
    {
        @XmlElement(name = "customerLocation")
        private List<CustomerLocation> customerLocations;

        public CustomerLocationsList(List<CustomerLocation> customerLocations) {
            this.customerLocations = customerLocations;
        }
        
        public CustomerLocationsList() {
            this.customerLocations = new ArrayList<CustomerLocation>();
        }

        public List<CustomerLocation> getCustomerLocations() {
            return customerLocations;
        }

        public void setCustomerLocations(List<CustomerLocation> customerLocations) {
            this.customerLocations = customerLocations;
        }
    }
}
